<?php

class Default_SearchController extends Ext_Controller_Action_Front
{
    public function init()
    {
        parent::init();
    }

    public function preDispatch() 
    {
        parent::preDispatch();
        
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        $roleId = Ext_User::getInstance()->show($userId)->buildRole()->role->roleId;
        $acl = Ext_Acl::getInstance()->build($roleId);
        if(!$acl->isAllowed($roleId, 'index', 'view')){
            $this->_forward("show-access-denied", "auth", "account");
            return false;
        }
    }
    
    public function tagAction()
    {
        $tagId   = $this->filter->filter($this->_request->get('tagId'));
        $tagName = $this->filter->filter($this->_request->get('tagName'));
        if($tagName){
            $tag = App_Model_DbTable_Tags::getInstance()->getRowByName($tagName);
            if($tag){
                $tagId = $tag['id'];
            }
        }
        
        if(!$tagId){

        }
        
        $tag = App_Model_DbTable_Tags::getInstance()->getRowById($tagId);
        if($tag)
        {
            $row = array(
                'tagId'      => $tagId,
                'createTime' => time(),
            );
            App_Model_DbTable_Tag_History::getInstance()->addNew($row);
            
            $tagName = $tag['tagName'];
        }
        
        $count = App_Model_Article::getInstance()->getSearchCountByTag($tagId);
        $list = App_Model_Article::getInstance()->getSearchListByTag($tagId);
        $adapter = new Zend_Paginator_Adapter_DbSelect($list);
        $adapter->setRowCount($count);
        $paginator = new Zend_Paginator($adapter);
        $paginator
              //->setCurrentPageNumber(1)
              ->setItemCountPerPage(12)
              ->setCurrentPageNumber($this->_getParam('page'))
            //->setDefaultPageRange(20)
            ;
        $articles = array();
        foreach ($paginator as $key=>$item)
        {
            $articleId = $item['articleId'];
            
            $article = Ext_Article::getInstance()->show($articleId)->isExcerpt()->buildDescription()->buildAttachemnts()->buildTags()->buildFamily();

            $articles[] = $article;
        }

        $this->view->pageTitle = $tagName;
        $this->view->tagName   = $tagName;
        $this->view->articles  = $articles;
        $this->view->paginator = $paginator;        
    }
    
    
}

?>